import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/modules/recipe/data/api/recipe_category_api.dart';
import 'package:casdon/modules/recipe/data/repository/recipe_repository.dart';
import 'package:casdon/modules/recipe/providers/recipe_provider.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class RecipeCategoryUsecase {
  final RecipeRepository _repository;

  const RecipeCategoryUsecase(this._repository);

  Future<ResponseRecipeCategoryApi> execute({
    String? id,
    required int pageNum,
    required int pageSize,
  }) async {
    try {
      ResponseRecipeCategoryApi? ret;
      if (id == null) {
        ret = await _repository.getRecipeCategory(pageNum, pageSize);
      } else {
        ret = await _repository.getRecipeCategoryWithId(id, pageNum, pageSize);
      }

      ret ??= ResponseRecipeCategoryApi(0, []);
      return ret;
    } on BusinessException {
      rethrow;
    } catch (e) {
      Log.e("ResponseRecipeCategoryApi 失败", e.toString(), StackTrace.current);
      throw Exception("ResponseRecipeCategoryApi 失败");
    }
  }
}

final recipeCategoryUsecaseProvider = Provider(
  (ref) => RecipeCategoryUsecase(ref.read(recipeRepository)),
);
